using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SecurePasswords
{
    public partial class EditPasswordForm : Form
    {
        public EditPasswordForm()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void EditPasswordForm_Closing(object sender, CancelEventArgs e)
        {
            //if all fields blank, cancel
            if( txtSite.Text.Trim().Equals(string.Empty) && txtUsername.Text.Trim().Equals(string.Empty) && txtPassword.Text.Trim().Equals(string.Empty) && txtNote.Text.Trim().Equals(string.Empty) ) {
                DialogResult = DialogResult.Cancel;
            }
        }

        /// <summary>
        /// Called whenever an input textfield needs to display input panel.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtBox_GotFocus(object sender, EventArgs e)
        {
            inputPanel1.Enabled = true;
        }

        /// <summary>
        /// Called whenever an input textfield no longer needs to display input panel.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtBox_LostFocus(object sender, EventArgs e)
        {
            inputPanel1.Enabled = false;
        }

        /// <summary>
        /// Called when the popup menu should appear. enables/disables the correct menu items
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmEditMenu_Popup(object sender, System.EventArgs e)
        {
            // Find control with focus
            TextBox activeTextBox = null;
            foreach (Control ctl in this.Controls)
            {
                if (ctl.Focused)
                {
                    if (ctl.GetType().Name == "TextBox")
                        activeTextBox = (TextBox)ctl;
                    break;
                }
            }

            if (activeTextBox != null)
            {
                if ( activeTextBox.CanUndo /*EditMenuHelper.CanUndo()*/)
                    miUndo.Enabled = true;
                else
                    miUndo.Enabled = false;

                if (activeTextBox.SelectionLength > 0)
                {
                    miCut.Enabled = true;
                    miCopy.Enabled = true;
                    miClear.Enabled = true;
                }
                else
                {
                    miCut.Enabled = false;
                    miCopy.Enabled = false;
                    miClear.Enabled = false;
                }
                if (EditMenuHelper.ClipboardHasText())
                    miPaste.Enabled = true;
                else
                    miPaste.Enabled = false;
            }
            else
            {
                miUndo.Enabled = false;
                miCut.Enabled = false;
                miCopy.Enabled = false;
                miPaste.Enabled = false;
                miClear.Enabled = false;
            }
        }

        private void miCopy_Click(object sender, EventArgs e)
        {
            EditMenuHelper.Copy();
        }

        private void miCut_Click(object sender, EventArgs e)
        {
            EditMenuHelper.Cut();
        }

        private void miPaste_Click(object sender, EventArgs e)
        {
            EditMenuHelper.Paste();
        }

        private void miClear_Click(object sender, EventArgs e)
        {
            EditMenuHelper.Clear();
        }

        private void miUndo_Click(object sender, EventArgs e)
        {
            EditMenuHelper.Undo();
        }

        private void inputPanel1_EnabledChanged(object sender, EventArgs e)
        {
            SuspendLayout();
            sipCoverPanel.Height = inputPanel1.Bounds.Height;
            sipCoverPanel.Visible = inputPanel1.Enabled;
            ResumeLayout();
        }

    }
}